package com.thoughtworks.biblioteca;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

//Understands the console operations needed for Biblioteca application.
public class BibliotecaConsole {
	public static final boolean CHECKOUT_SUCCESSFUL = true;
	InputStreamReader inputStream = new InputStreamReader(System.in);
	BufferedReader reader = new BufferedReader(inputStream);

	public void displayMainMenu(boolean isLibrarianLoggedIn) {
		System.out
				.println("Welcome to The Bangalore Public Library System - Biblioteca");
		System.out.println("Menu");
		System.out.println("1. List Book Catalog");
		System.out.println("2. Check out Book");
		System.out.println("3. Check Library Number");
		System.out.println("4. List Movie Catalog");
		System.out.println("5. Rate Movies");
		System.out.println("6. Request Movie");
		System.out.println("7. Requested Movies");
		displayAdditionalOptionsForLibrarian(isLibrarianLoggedIn);
		System.out.println("Enter your Selection: ");
	}

	private void displayAdditionalOptionsForLibrarian(
			boolean isLibrarianLoggedIn) {
		System.out.println("8. "
				+ (isLibrarianLoggedIn ? "Add Movies\n9. Close Request\n10. "
						: "") + "Exit");
	}

	public void displayBooksCatalog(BooksCatalog booksCatalog) {
		List<Book> bookList = booksCatalog.getBookCatalog();
		for (int i = 0; i < bookList.size(); i++) {
			System.out.println(i + 1 + ". " + bookList.get(i).getBookDetails());
		}
	}

	public void displayMovieCatalog(MoviesCatalog moviesCatalog,
			boolean isUserLoggedIn, String loggedUser) {
		MovieDB movieDB = new MovieDB();
		List<Movie> movieList = moviesCatalog.getMovieCatalog();
		displayMovieTableHeader(isUserLoggedIn);
		for (int movieNumber = 0; movieNumber < movieList.size(); movieNumber++) {
			displayMovieDetails(isUserLoggedIn, loggedUser, movieDB, movieList,
					movieNumber);
		}

	}

	private void displayMovieDetails(boolean isUserLoggedIn, String loggedUser,
			MovieDB movieDB, List<Movie> movieList, int movieNumber) {
		displayForAllUsers(movieList, movieNumber);
		additionalDisplayForLoggedUsers(isUserLoggedIn, loggedUser, movieDB,
				movieList, movieNumber);
	}

	private void additionalDisplayForLoggedUsers(boolean isUserLoggedIn,
			String loggedUser, MovieDB movieDB, List<Movie> movieList,
			int movieNumber) {
		if (isUserLoggedIn) {
			System.out.format("%-15s\n", movieDB.movieUserRating(loggedUser,
					movieList.get(movieNumber).getMovieName()));
		} else {
			System.out.println();
		}
	}

	private void displayForAllUsers(List<Movie> movieList, int movieNumber) {
		System.out.format("%-25s%-25s%-15s", movieList.get(movieNumber)
				.getMovieName(), movieList.get(movieNumber).getMovieDirector(),
				movieList.get(movieNumber).getMovieRating());
	}

	private void displayMovieTableHeader(boolean isUserLoggedIn) {
		System.out
				.format("%-25s%-25s%-15s", "Movie Name", "Director", "Rating");
		additionalHeaderForLoggedUsers(isUserLoggedIn);
	}

	private void additionalHeaderForLoggedUsers(boolean isUserLoggedIn) {
		if (isUserLoggedIn) {
			System.out.format("%-15s\n", "Your Rating");
		} else {
			System.out.println();
		}
	}

	public void displayCheckoutMessage(boolean checkoutStatus) {
		if (checkoutStatus == CHECKOUT_SUCCESSFUL) {
			System.out.println("\nThank You! Enjoy the book.");
		} else {
			System.out.println("\nSorry we don't have that book yet.");
		}
	}

	public void displayRequestedMovieDetails(String movieName, MovieDB movieDB,
			Movie requestedMovie, String loggedUser) {
		System.out.println("\nMovie Details:");
		System.out.println("Name: " + requestedMovie.getMovieName());
		System.out.println("Director: " + requestedMovie.getMovieDirector());
		System.out.println("Average User Rating: "
				+ requestedMovie.getMovieRating());
		System.out.println("Your Rating:"
				+ movieDB.movieUserRating(loggedUser, movieName));
	}

	public void displayRatingsMenu() {
		System.out.println("\nRatings Menu");
		System.out.println("1-Give Rating");
		System.out.println("2-Go Back");
		System.out.println("Enter your choice:");
	}

	public void displayRequestsMenu() {
		System.out.println("\nRequests Menu");
		System.out.println("1-Confirm Request");
		System.out.println("2-Go Back");
		System.out.println("Enter your choice:");
	}

	public void displayUserNameMessage(String generatedUserName) {
		if (generatedUserName != null) {
			System.out.println("Your Library Number (Username) is:"
					+ generatedUserName + "\n");
		} else {
			System.out.println("\nPlease talk to Librarian. Thank you.");
		}
	}

	public int getInputFromUser() {
		int userInput = 0;
		try {
			userInput = Integer.parseInt(reader.readLine());
		} catch (Exception e) {
			System.out.println("Input Error!!");
		}
		return userInput;
	}

	public String getNameOfTheMovie() {

		System.out.println("Enter the name of the movie:");
		String movieName = null;
		try {
			movieName = new String(reader.readLine());
		} catch (Exception e) {
			System.out.println("Enter a valid string!!");
		}
		return movieName;
	}

	public String readLoginInput() {
		String input = null;
		try {
			input = new String(reader.readLine());

		} catch (Exception e) {
			System.out.println("Enter a valid string!!");
		}
		return input;
	}

	void displayWrongRatingMessage() {
		System.out.println("Rating should be between 1 and 10 inclusive.");
		System.out.println("Rating is not saved.");
	}

	int getRatingForMovie() {
		System.out.println("Enter rating for this movie:");
		int userRating = 0;
		userRating = getInputFromUser();
		return userRating;
	}

	int passwordSetMenu() {
		System.out.println("Password is not set!");
		System.out.println("Enter 1 to try again. 0 to Exit");
		int passwordChoice = getInputFromUser();
		return passwordChoice;
	}

	String readLoginUserName() {
		String userName;
		System.out
				.println("You have to login to access this feature of the library");
		System.out.println("Enter your Library Number:");
		userName = readLoginInput();
		return userName;
	}

	String readLoginPassword() {
		String password;
		System.out.println("Enter your password:");
		password = readLoginInput();
		return password;
	}

	int wrongPasswordMenu() {
		System.out.println("Wrong Username or password.");
		System.out.println("Enter 1 to try login again. 0 to go back");
		return getInputFromUser();
	}

	void displayNewUserPasswordRules() {
		System.out.println("You have to set password now to login.");
		System.out
				.println("Password should be between 6 and 10 characters long");
	}

	void setReader(BufferedReader bufferedReader) {
		this.reader = bufferedReader;
	}

	int getBookToCheckOut() {
		System.out
				.println("Please enter the number of the book you wish to checkout: ");
		int bookNumber = 0;
		bookNumber = getInputFromUser();
		return bookNumber;
	}

	public String getMovieDirector() {
		System.out.println("Enter the name of the director:");
		String movieDirector = null;
		try {
			movieDirector = new String(reader.readLine());
		} catch (Exception e) {
			System.out.println("Enter a valid string!!");
		}
		return movieDirector;
	}

}
